<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 在线解答逻辑
 */
class Answer extends AdminBase
{
    

    
    /**
     * 获取在线解答列表
     */
    public function getArticleList($where = [], $field = 'a.*', $order = '')
    {
        
        $this->modelCourseQuestion->alias('a');

        $join = [
            [SYS_DB_PREFIX . 'question_category c', 'a.cate_id = c.id'],
        ];

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        $this->modelCourseQuestion->join = $join;
        
        return $this->modelCourseQuestion->getList($where, $field, $order);
    }
    
    /**
     * 获取在线解答列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.name|a.answer'] = ['like', '%'.$data['search_data'].'%'];
        !empty($data['cate_id'])  && $where['a.cate_id'] =$data['cate_id'];
        return $where;
    }
    
    /**
     * 在线解答信息编辑
     */
    public function articleEdit($data = [])
    {

        $validate_result = $this->validateQuestion->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateQuestion->getError()];
        }
        $url = url('articleList');
        if(empty($data['id'])){
            unset($data['id']);
        }

       // empty($data['id']) && $data['member_id'] = MEMBER_ID;
        
//      $data['content'] = htmlspecialchars_decode($data['content']);
        $result = $this->modelCourseQuestion->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '在线解答' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '在线解答操作成功', $url] : [RESULT_ERROR, $this->modelCourseQuestion->getError()];
    }

    /**
     * 获取在线解答信息
     */
    public function getArticleInfo($where = [], $field = 'a.*,m.nickname')
    {
        
        $this->modelCourseQuestion->alias('a');
        

        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        

        
        return $this->modelCourseQuestion->getInfo($where, $field);
    }
    

    
    /**
     * 在线解答删除
     */
    public function articleDel($where = [])
    {
        
        $result = $this->modelCourseQuestion->deleteInfo($where);
        
        $result && action_log('删除', '在线解答删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '在线解答删除成功'] : [RESULT_ERROR, $this->modelCourseQuestion->getError()];
    }

    /**
     * 在线解答删除
     */
    public function update_info($where = [])
    {

        //dd($where);

        $result = $this->modelCourseQuestion->setInfo($where);


        return $result ? [RESULT_SUCCESS, '操作成功'] : [RESULT_ERROR, '操作失败'];
    }


    /**
     * 获取分类信息
     */
    public function getArticleCategoryInfo($where = [], $field = true)
    {

        return $this->modelQuestionCategory->getInfo($where, $field);
    }

    /**
     * 获取问题分类列表
     */
    public function getArticleCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {
        $count=$this->modelQuestionCategory->where($where)->count();
        $list=$this->modelQuestionCategory->getList($where, $field, $order, $paginate);
        return ['count'=>$count,'list'=>$list];
    }

    /**
     * 问题分类删除
     */
    public function articleCategoryDel($where = [])
    {

        $result = $this->modelQuestionCategory->deleteInfo($where);

        $result && action_log('删除', '问题分类删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '问题分类删除成功'] : [RESULT_ERROR, $this->modelQuestionCategory->getError()];
    }

    /**
     * 文章分类编辑
     */
    public function articleCategoryEdit($data = [])
    {



        $url = url('articleCategoryList');
        $result = $this->modelQuestionCategory->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '问题分类' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelQuestionCategory->getError()];
    }

}
